Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

384
Vistas
How to compile Kotlin code to jvmTarget="1.8" for tests

In our Android project we code in Kotlin and target java 1.6. However we are forced to java 1.8 in our test since some JUnit5 features requires it (static methods in interfaces).

Is it possibe to compile the tests differently than the production code?

We tried to raise the jvmTarget to 1.8 by adding this to our build.gradle:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
        kotlinOptions {
            jvmTarget = "1.8"
        }
}

This raises the jvmTarget of the production code too, but we only want it for our tests. The Docs indicate that it can be specified for the test like this:

compileReleaseUnitTestKotlin{
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

Unfortunatly the build.gradle doesn't compile.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

The Kotlin docs mention how to do it for tests only.

Groovy DSL:

compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

Kotlin DSL:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

val compileTestKotlin: KotlinCompile by tasks

compileTestKotlin.kotlinOptions.jvmTarget = "1.8"
over 4 years ago · Santiago Trujillo Denunciar

0

This snippet does the job for me:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
    if (it.name.contains("UnitTest")) {
        kotlinOptions {
            jvmTarget = "1.8"
        }
    }
}

I have to say, It's not the best solution to filter the test tasks based on their names, so think of this as a quick and dirty aid.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda